package pl.kane.autokomp.applications.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;



import org.apache.log4j.Logger;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;



import pl.kane.autokomp.applications.MainFrame;
import pl.kane.autokomp.applications.dto.MenuData;



public class MenuUtilLoader {

	private static Logger logger = Logger.getLogger(MenuUtilLoader.class);
	
	private static MenuData menu;
	
	public static MenuData loadMenu(String filePath) {
		Mapping mapping = new Mapping();
		byte[] buffer;
		int size = -1;
		MenuData menu=null;
				
    	try {
			mapping.loadMapping(MenuUtilLoader.class.getResource(filePath+"MenuMapping.xml"));
			
			File f = new File("Data/Menu.xml");
			buffer = new byte[(int)f.length()+200];
			FileInputStream fis = new FileInputStream(f);
			size = fis.read(buffer);
			
			Unmarshaller unmar = new Unmarshaller(mapping);
			ByteArrayInputStream input = new ByteArrayInputStream(buffer, 0, size);
			menu = (MenuData)unmar.unmarshal(new InputStreamReader(input));
			
		} catch (MarshalException e) {
			
			e.printStackTrace();
		} catch (ValidationException e) {
			
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
				
			e.printStackTrace();
		} catch (MappingException e) {
			
			e.printStackTrace();
		}
		return menu;
	}
	public static void main(String [ ] args) {
		MenuData menu=MenuUtilLoader.loadMenu("/pl/kane/autokomp/applications/utils/");
	}
	public static MenuData getMenu() {
		if (menu==null) 
			menu = loadMenu("/pl/kane/autokomp/applications/utils/");
		return menu;
	}
}
